package com.example.news.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.news.mapper.CategoryMapper;
import com.example.news.pojo.entity.Category;
import com.example.news.pojo.form.BasePageQuery;
import com.example.news.pojo.form.CategoryForm;
import com.example.news.pojo.vo.CategoryVo;
import com.example.news.service.CategoryService;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category>
        implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Result<String> createCategory(CategoryForm request){
        Category category = new Category();
        category.setName(request.getName());
        category.setStatus(1);
        categoryMapper.insert(category);
        return Result.ok(null,"类型创建成功");

    }

    @Override
    public PageResult<CategoryVo> getAllCategory(BasePageQuery basePageQuery){
        Page<Category> page = new Page<>(basePageQuery.getPageNum(),basePageQuery.getPageSize());
        IPage<CategoryVo> iPage =  categoryMapper.getAllCategories(page);
        return PageResult.success(iPage);
    }

    @Override
    public Result<String> modifyCategory(CategoryForm request) {
        Category category = new Category();
        BeanUtils.copyProperties(request,category);

        categoryMapper.updateById(category);
        return Result.ok(null,"类型修改成功");
    }

    @Override
    public Result<String> deleteCategory(Integer id){
        categoryMapper.deleteById(id);
        return Result.ok(null,"类型删除成功");
    }

    @Override
    public PageResult<Category> userGetAllCategory(BasePageQuery basePageQuery){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,1);

        Page<Category> page = new Page<>(basePageQuery.getPageNum(),basePageQuery.getPageSize());
        IPage<Category> iPage =  categoryMapper.selectPage(page,queryWrapper);
        return PageResult.success(iPage);
    }
}
